<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/28
 * Time: 16:39
 */
namespace App\Modules\User\Models;

use Illuminate\Database\Eloquent\Model;
use DB;
use Illuminate\Support\Facades\Log;

class UserTaskDetail extends Model{
    /**
     * 获取用户任务信息
     * @param $uid 用户id
     */
    static function getUserTaskInfo($uid)
    {
        $rewardTimes = self::getUserRewardsTimes($uid);
        $task_num = 0;
        $rewardCount=0;
    }
    /**
     * 获取所有用户平均打赏信息
     */
    static function getAverageTaskInfo(){

    }

    /**
     * 获取用户打赏次数及打赏金额
     * @param $uid
     * @return mixed
     */
    static function getUserRewardsInfo($uid){

        $count = DB::table('self_reco_suc_chains as srsc')
            ->join('tasks as t', 't.id', '=', 'srsc.task_id')
            ->select(DB::raw("count('srsc.id') as rewards,sum(srsc.fee) as total_rewards"))
            ->where([
                ['t.uid','=',$uid],
                ['srsc.pay_status','=','SUC_REWARD']
            ])
            ->first();
        $rewardTimes = $count->rewards;
        $rewardTotal = $count->total_rewards;

        return ['rewardTimes'=>$rewardTimes,'rewardTotal'=>$rewardTotal];
    }

    /**
     * 获取用户所有已发布的任务数
     * @param $uid
     * @return mixed
     */
    static function getUserTaskNum($uid) {

        $count = DB::table('tasks')
            ->select(DB::raw('count(id) as cid'))
            ->where('uid','=',$uid)
            ->first();
        $task_num = $count->cid;

        return $task_num;
    }

    /**
     * 获取用户感兴趣总数
     */
    static function getUserPickTotal($uid){
        $count = DB::table('self_recommendations as sr')
            ->leftjoin('tasks as t','t.id','=','sr.task_id')
            ->select(DB::raw('count(sr.id) as cid'))
            ->where('t.uid','=',$uid)
            ->wherein('sr.status',['PROCESSING','NORESPONSE','SUCCESS'])
            ->first();
        $interest = $count->cid;

        return $interest;
    }

    static function getMaxReward($uid){
        $count = DB::table('self_reco_suc_chains as srsc')
            ->leftjoin('tasks as t','t.id','=','srsc.task_id')
            ->select(DB::raw('max(srsc.fee) as max_fee'))
            ->where('t.uid','=',$uid)
            ->first();
        $peakReward = $count->max_fee;
        return $peakReward;
    }

    /**
     * 获取用户处理总时长
     * @param $uid
     */
    static function handleDuration($uid){
        $sql = "select unix_timestamp(sr.view_time)-unix_timestamp(sr.created_at) as diff_time,sr.id from `self_recommendations` as sr inner join `tasks` ts on ts.id = sr.task_id where ts.`uid` = $uid AND sr.`is_view` = 1";
        $res=DB::table(DB::raw("($sql) as tb_main"))
            ->select(DB::raw("sum(tb_main.diff_time) as sum_diff_time,count(tb_main.id) as countall"))
            ->first();
        /*$sqlinfo = DB::table(DB::raw("($sql) as tb_main"))
            ->select(DB::raw("sum(tb_main.diff_time) as sum_diff_time,count(tb_main.id) as countall"))
            ->toSql();
        Log::info('handleDuration'.$sqlinfo);*/
        $totalTime = $res->sum_diff_time;//->diff_time;
        $totalCount = $res->countall;
        return ['totalTime'=>$totalTime,'totalCount'=>$totalCount];
    }

    /**
     * 获取用户平均打赏
     * @param $uid 打赏发起者人
     * @return array
     */
    static function getAverageReward($uid){
        $res = DB::table("reward_details")
            ->select(DB::raw("sum(fee) as fee,count(id) as cid"))
            ->where('pay_uid','=',$uid)
            ->first();
        return ['RewardToUserSum'=>$res->fee,'RewardToUserNum'=>$res->cid];
    }

    /**
     * 获取用户被打赏次数
     * @param $uid
     * @return mixed
     */
    static function getRewardedTimes($uid){
        $res = DB::table("reward_details")
            ->select(DB::raw('count(id) as cid'))
            ->where('uid',$uid)
            ->first();
        return $res->cid;
    }

    /**
     * 獲取用戶承諾酬金
     * @param $uid
     * @return mixed
     */
    static function getPromiseRewards($uid){
        $res = DB::table("tasks")
            ->where('uid',$uid)
            ->sum('fee');
        return $res;
    }

    static function getSponsorViewRecoNum($uid){
        $res = DB::table("self_recommendations as re")
            ->leftjoin("tasks as t",'t.id','=','re.task_id')
            ->select(DB::raw("count(re.id) as cid"))
            ->where([['t.uid','=',$uid],['re.is_view','=',1]])
            ->first();

        $res=$res?$res->cid:0;
        return $res;
    }
}